package controllers

import (
	"LoveU/models"
	"LoveU/utils"

	"github.com/astaxie/beego"
)

type LoginController struct {
	beego.Controller
}

// 参数
// 当exit参数的值为true
func (this *LoginController) Get() {
	this.TplName = ""
}

//用户登录
func (this *LoginController) Login() {
	name := this.Input().Get("name")
	pwd := this.Input().Get("pwd")
	status,user := models.GetUser(name, pwd)
	var codejson interface{}
	if status==utils.LOGIN_OK {
		userSession, _ := globalSessions.SessionStart(this.Ctx.ResponseWriter, this.Ctx.Request)
		defer userSession.SessionRelease(this.Ctx.ResponseWriter)
		codejson = utils.Message{utils.LOGIN_OK, "success", nil, userSession.SessionID()}
		SessionStore[userSession.SessionID()] = userSession
		userSession.Set("sessionid", userSession.SessionID())
		userSession.Set("name", name)
		userSession.Set("pwd", pwd)
		userSession.Set("userid", user.Id)
	} else if status==utils.USER_FROZEN {
		codejson = utils.Message{utils.USER_FROZEN, "用户账号已被冻结", nil, ""}
	} else if status==utils.LOGIN_PWDWRONG {
		codejson = utils.Message{utils.USER_FROZEN, "密码不正确", nil, ""}
	} else {
		codejson = utils.Message{utils.USER_NOT_FOUND, "用户不存在", nil, ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

//用户退出登录
func (this *LoginController) Logout() {
	var codejson interface{}
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession != nil {
		mySession.Flush()
		globalSessions.SessionDestroy(this.Ctx.ResponseWriter,this.Ctx.Request)
		delete(SessionStore,sid)
		codejson = utils.Message{utils.LOGOUT_OK, "success", nil, ""}
	} else {
		codejson = utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
}

// 检查用户名使用状态
func (this *LoginController) CheckName() {
	name := this.Input().Get("name")
	var codejson interface{}
	status := models.CheckName(name)
	switch status {
	case 400:
		codejson = utils.Message{utils.REG_NAME_OK, "success", nil, ""}
	case utils.REG_NAME_EXISTS:
		codejson = utils.Message{utils.REG_PHONE_EXISTS, "用户名已存在", nil, ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
}

// 用户初始注册
func (this *LoginController) Register() {
	name := this.Input().Get("name")
	pwd := this.Input().Get("pwd")
	phone := this.Input().Get("phone")
	userSession, _ := globalSessions.SessionStart(this.Ctx.ResponseWriter, this.Ctx.Request)
	status := models.Register(name, phone, pwd)
	// beego.Error(status)
	var codejson interface{}
	switch status {
	case 10:
		//注册成功，同时填写默认的详细信息（忽略数据库错误）
		_,user := models.GetUser(name,pwd)
		if user!=nil {
			models.RegisterDefault(user.Id)
		}
		codejson = utils.Message{utils.REG_OK, "success", nil, userSession.SessionID()}
	case utils.REG_NAME_EXISTS:
		codejson = utils.Message{utils.REG_NAME_EXISTS, "用户名已存在", nil, ""}
	case utils.REG_PHONE_EXISTS:
		codejson = utils.Message{utils.REG_PHONE_EXISTS, "手机已经被注册", nil, ""}
	case 10001:
		codejson = utils.Message{utils.UNKNOWN_ERROR, "注册失败，未知错误", nil, ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

// 检查是否处于登录状态
func (this *LoginController) CheckLogin() {
	var codejson interface{}
	sid := this.Input().Get("sessionid")
	mySession := SessionStore[sid]
	if mySession == nil {
		codejson = utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	} else {
		codejson = utils.Message{utils.LOGGED_IN, "success", nil, ""}
	}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}
